#include "playscence.h"
#include<QDebug>
#include<QMenuBar>
#include"chooselevelscence.h"
#include<QPainter>
#include"mypushbutton.h"
#include<QTimer>
#include<QLabel>
#include"mycoin.h"
#include"dataconfig.h"
#include<QPropertyAnimation>
#include<QSound>  //Coin flips // QT       += core gui multimedia
//PlayScence::PlayScence(QWidget *parent) : QMainWindow(parent)
//{

//}

PlayScence::PlayScence(int levelNum)
{
    QString str=QString("进入了第%1关卡").arg(levelNum);
    qDebug()<<str;
    this->levelIndex=levelNum;


    setFixedSize(320,590);
    setWindowIcon(QPixmap(":/res/Coin0001.png"));
    setWindowTitle("MyLevelSwitch");

    QMenuBar*barChooseScence=menuBar();
    setMenuBar(barChooseScence);

    QMenu*MainMenu=barChooseScence->addMenu("菜单");

    QAction*createAction=MainMenu->addAction("新建");
    QAction*openAction=MainMenu->addAction("打开");
    QAction*storyAction=MainMenu->addAction("存储");
    QAction*backAction=MainMenu->addAction("返回");

    QAction*quitAction=MainMenu->addAction("退出");
    connect(quitAction,&QAction::triggered,[=](){
        this->close();
    });

    //添加音效资源
     QSound*backSound=new QSound(":/res/BackButtonSound.wav",this);
       QSound*flipSound=new QSound(":/res/ConFlipSound.wav",this);
         QSound*winSound=new QSound(":/res/LevelWinSound.wav",this);

    //返回按钮
    MyPushButton*backBtn=new MyPushButton(":/res/BackButton.png",":/res/BackButtonSelected.png");
    backBtn->setParent(this);
    backBtn->move(this->width()-backBtn->width(),this->height()-backBtn->height());

    connect(backBtn,&MyPushButton::clicked,[=](){
        backSound->play();
       qDebug()<<"Press rebackButton";
        //告诉主场景，返回按钮
       QTimer::singleShot(500,this,[=](){
       emit this->chooseScenceBack();

        });

    });

    //显示当前关卡
    QLabel*label=new QLabel;
    label->setParent(this);
    QFont font;
    font.setFamily("华文新魏");
    font.setPointSize(20);
    QString str1=QString("Level:%1").arg(this->levelIndex);
    label->setFont(font);
    label->setText(str1);
    label->setGeometry(30,this->height()-50,120,50);

    //初始化每个关卡的二维数组
    dataConfig config;
    for(int i=0;i<4;i++)
    {
        for(int j=0;j<4;j++)
        {
            this->gameArray[i][j]=config.mData[this->levelIndex][i][j];

        }
    }

    //胜利图片显示
    QLabel*winLabel=new QLabel;
    QPixmap tmpPix;
    tmpPix.load(":/res/LevelCompletedDialogBg.png");
    winLabel->setGeometry(0,0,tmpPix.width(),tmpPix.height());
    winLabel->setPixmap(tmpPix);
    winLabel->setParent(this);
    winLabel->move((this->width()-tmpPix.width())*0.5,-tmpPix.height());



    for(int i=0;i<4;i++)
    {
        for(int j=0;j<4;j++)
        {
           QPixmap pix=QPixmap(":/res/BoardNode.png") ;
           QLabel*label=new QLabel;
           label->setGeometry(0,0,pix.width(),pix.height());
           label->setPixmap(pix);
           label->setParent(this);
           label->move(57+i*50,200+j*50);

           QString str;

           if(this->gameArray[i][j]==1)
           {
               str=":/res/Coin0001.png";
           }
           else
           {
              str=":/res/Coin0008.png";
           }
           MyCoin*coin=new MyCoin(str);
           coin->setParent(this);
           coin->move(59+i*50,204+j*50);

           //给金币属性赋值
           coin->posX=i;
           coin->posY=j;
           coin->flag=this->gameArray[i][j];

           //将金币放入到金币的二维数组 以便后期维护
           coinBtn[i][j]=coin;

           //点击金币翻转
           connect(coin,&MyCoin::clicked,[=](){
               //播放返金币音效
               flipSound->play();
               //点击按钮，金币的二位数字里，以便后期维护
               for(int i=0;i<4;i++)
               {
                   for(int j=0;j<4;j++)
                   {
                       this->coinBtn[i][j]->isWin=true;
                   }
               }
               coin->changeFlag();
               this->gameArray[i][j]=this->gameArray[i][j]==0?1:0;

            //翻转周围
               QTimer::singleShot(300,this,[=](){
                   if(coin->posX+1<=3)
                   {
                       coinBtn[coin->posX+1][coin->posY]->changeFlag();
                       this->gameArray[coin->posX+1][coin->posY]=this->gameArray[coin->posX+1][coin->posY]==0?1:0;
                   }
                   if(coin->posX-1>=0)
                   {
                       coinBtn[coin->posX-1][coin->posY]->changeFlag();
                       this->gameArray[coin->posX-1][coin->posY]=this->gameArray[coin->posX-1][coin->posY]==0?1:0;
                   }

                   if(coin->posY+1<=3)
                   {
                       coinBtn[coin->posX][coin->posY+1]->changeFlag();
                       this->gameArray[coin->posX][coin->posY+1]=this->gameArray[coin->posX][coin->posY+1]==0?1:0;
                   }

                   if(coin->posY-1>=0)
                   {
                       coinBtn[coin->posX][coin->posY-1]->changeFlag();
                       this->gameArray[coin->posX][coin->posY-1]=this->gameArray[coin->posX][coin->posY-1]==0?1:0;
                   }
                   //翻完周围金币后，将所有金币解开禁用
                   for(int i=0;i<4;i++)
                   {
                       for(int j=0;j<4;j++)
                       {
                           this->coinBtn[i][j]->isWin=false;
                       }
                   }


                   //判断是否胜利
                   this->isWin=true;
                   for(int i=0;i<4;i++)
                   {
                       for(int j=0;j<4;j++)
                       {
                           if(coinBtn[i][j]->flag==false)
                           {
                               this->isWin=false;
                               break;
                           }
                       }
                   }


                   if(this->isWin==true)
                   {
                       //添加胜利音效
                       winSound->play();
                       qDebug()<<"游戏胜利";
                       //将所有胜利的标志改成ture;
                       for(int i=0;i<4;i++)
                       {
                           for(int j=0;j<4;j++)
                           {
                                   coinBtn[i][j]->isWin=true;
                           }
                        }
                       //经胜利的图片移动下来
                       QPropertyAnimation*animation=new QPropertyAnimation(winLabel,"geometry");

                       //设置时间间隔
                       animation->setDuration(1000);
                       //设置开始位置
                       animation->setStartValue(QRect(winLabel->x(),winLabel->y(),winLabel->width(),winLabel->height()));
                       //设置结束位置
                       animation->setEndValue(QRect(winLabel->x(),winLabel->y()+114,winLabel->width(),winLabel->height()));
                       //设置返回曲线
                       animation->setEasingCurve(QEasingCurve::OutBounce);
                       animation->start();



                   }

               });
           });
        }
    }




}

     void PlayScence:: paintEvent(QPaintEvent * )
    {
        QPainter painter(this);
        QPixmap pix;
        pix.load(":/res/PlayLevelSceneBg.png");
        painter.drawPixmap(0,0,this->width(),this->height(),pix);

        pix.load(":/res/Title.png");
        pix=pix.scaled(pix.width()*0.5,pix.height()*0.5);
        painter.drawPixmap(10,30,pix.width(),pix.height(),pix);

    }



